package com.ylsk.statistics.service.impl;

import com.ylsk.check.domain.emum.ReportStatusEnum;
import com.ylsk.check.domain.so.CheckRecordSO;
import com.ylsk.check.domain.so.ReportInfoSO;
import com.ylsk.check.domain.vo.ReportInfoVO;
import com.ylsk.check.service.IReportInfoService;
import com.ylsk.common.annotation.DataScope;
import com.ylsk.statistics.domain.vo.CheckCountTotalVO;
import com.ylsk.statistics.domain.vo.ProjectProcessTotalVO;
import com.ylsk.statistics.mapper.ReportFormsMapper;
import com.ylsk.statistics.service.ReportFormsService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportFormsServiceImpl implements ReportFormsService {
    @Autowired
    private ReportFormsMapper reportFormsMapper;

    @Autowired
    private IReportInfoService reportInfoService;

    @Override
    public List<Map<String, Object>> getProjectTimeNodeInfo(Long reportInfoId) {
        return reportFormsMapper.getProjectTimeNodeInfo(reportInfoId);
    }

    @Override
    public List getProjectApprStatusTotal() {
        List<ReportInfoVO> reportInfoVOList = reportInfoService.selectreportInfoList(new ReportInfoSO());
        if(CollectionUtils.isEmpty(reportInfoVOList)) return Lists.newArrayList();
        List retList = Lists.newArrayList();
        Object[] defaultTitle = new Object[]{"product","待接收", "通过","驳回"};
        retList.add(defaultTitle);
        // 先按照大队的纬度进行统计
        Map<String, List<ReportInfoVO>> listMap = reportInfoVOList.stream().collect(Collectors.groupingBy(ReportInfoVO::getDeptName, LinkedHashMap::new,Collectors.toList()));

        for (String s : listMap.keySet()) {
            Integer submit = listMap.get(s).stream().filter(item -> item.getReportStatus() == ReportStatusEnum.SUBMIT.getCode()).collect(Collectors.toList()).size();
            Integer pass = listMap.get(s).stream().filter(item -> item.getReportStatus() == ReportStatusEnum.PASS.getCode()).collect(Collectors.toList()).size();
            Integer reject = listMap.get(s).stream().filter(item -> item.getReportStatus() == ReportStatusEnum.REJECT.getCode()).collect(Collectors.toList()).size();
            Object[] objects = new Object[]{s,submit,pass,reject};
            retList.add(objects);
        }
        return retList;
    }

    @Override
    @DataScope(deptAlias = "sd")
    public List<ProjectProcessTotalVO> getProjectProcessTotal(ReportInfoSO reportInfoSO) {
        return reportFormsMapper.getProjectProcessTotal(reportInfoSO);
    }

    @Override
    @DataScope(deptAlias = "sd")
    public List<CheckCountTotalVO> getCheckCountTotal(CheckRecordSO checkRecordSO) {
        return reportFormsMapper.getCheckCountTotal(checkRecordSO);
    }
}
